package bearworker.zxl.utils;

import bearworker.zxl.entity.ManagerMenu;
import bearworker.zxl.entity.extra.UserMenuEntity;
import bearworker.zxl.service.SystemService;
import com.google.gson.Gson;
import org.apache.log4j.Logger;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 逍遥的熊 on 2017/12/2.
 */
@Component
@Aspect
public class PriviUtil {

    private Logger logger = Logger.getLogger(PriviUtil.class);

    private Gson gson = new Gson();

    @Autowired
    private SystemService systemService;


    @AfterReturning("execution(* bearworker.zxl.dao.mapper.Manager*.insert*(..)) || execution(* bearworker.zxl.dao.mapper.Manager*.update*(..)) || execution(* bearworker.zxl.dao.mapper.Manager*.delete*(..))")
    public void updUserPriv(){
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("parentId",0);
        List<UserMenuEntity> list = systemService.getUserPriv(paramMap);

        Map<String,List<PriviMain>> userPrivMap = new HashMap<>();


        for(UserMenuEntity entity : list){
            List<PriviMain> mains = new ArrayList<>();
            PriviMain priviMain = new PriviMain();


            priviMain.setMenuId(entity.getMenuId());
            priviMain.setParName(entity.getMenuName());

            List<PriviDetail> details = new ArrayList<>();

            paramMap = new HashMap<>();
            paramMap.put("userId",entity.getUserId());
            paramMap.put("parentId",entity.getMenuId());
            List<UserMenuEntity> list2 = systemService.getUserPriv(paramMap);

            for(UserMenuEntity entity2 : list2){
                PriviDetail priviDetail = new PriviDetail();
                priviDetail.setMenuName(entity2.getMenuName());
                priviDetail.setUrl(entity2.getMethod());
                details.add(priviDetail);
            }

            priviMain.setDetails(details);
            mains.add(priviMain);
            if(userPrivMap.containsKey(entity.getUserId()+"_privi")) {
                userPrivMap.get(entity.getUserId()+"_privi").add(priviMain);
            }else {
                userPrivMap.put(entity.getUserId()+"_privi",mains);
            }
        }

        for(Map.Entry entry : userPrivMap.entrySet()) {
            RedisUtil.set(entry.getKey().toString(),gson.toJson(entry.getValue()));
        }

        this.updUrl();

    }

    public void updUrl(){
        List<ManagerMenu> list = systemService.getAll();
        Map<String,Object> map = new HashMap<>();

        for(ManagerMenu entity : list){
            Map<String,String> amap = new HashMap<>();
            amap.put("name",entity.getMenuName());
            amap.put("method",entity.getMethod());
            amap.put("url",entity.getUrl());
            amap.put("isLogin",entity.getIsLogin().toString());
            map.put(entity.getMenuKey(),amap);
        }

        RedisUtil.set("manager",gson.toJson(map));

    }

    class PriviMain{
        private String parName;
        private Integer menuId;
        private List<PriviDetail> details;

        public String getParName() {
            return parName;
        }

        public void setParName(String parName) {
            this.parName = parName;
        }

        public List<PriviDetail> getDetails() {
            return details;
        }

        public void setDetails(List<PriviDetail> details) {
            this.details = details;
        }

        public Integer getMenuId() {
            return menuId;
        }

        public void setMenuId(Integer menuId) {
            this.menuId = menuId;
        }
    }

    class PriviDetail{
        private String menuName;
        private String url;

        public String getMenuName() {
            return menuName;
        }

        public void setMenuName(String menuName) {
            this.menuName = menuName;
        }

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }
    }
}
